package com.jcf.springboot.controller.business;


import com.jcf.springboot.common.R;
import com.jcf.springboot.common.utils.DownloadUtils;
import com.jcf.springboot.entity.business.FilmArea;
import com.jcf.springboot.service.FilmAreaService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-03-29
 */
@CrossOrigin
@RestController
@RequestMapping("/filmarea")
public class FilmAreaController {

    @Resource
    private FilmAreaService filmAreaService;

    @GetMapping("/getList")
    public R getList(){
        List<FilmArea> list = filmAreaService.list(null);
        if(list.size()>0){
            return R.ok().data(list).message("获取区域列表成功");
        }
        return R.error().message("获取区域列表失败");
    }

    @PostMapping("/importExcel")
    public R upload(MultipartFile file) throws IOException {
        XSSFWorkbook sheets = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        List<FilmArea> areaList = new ArrayList<>();

        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            List<String> list = new ArrayList<>();
            XSSFRow row = sheetAt.getRow(i);
            short lastCellNum = row.getLastCellNum();
            System.out.println(lastCellNum);
            for (int j = 0; j < lastCellNum; j++) {
                XSSFCell cell = row.getCell(j);
//                cell.setCellType(CellType.STRING);
                String value = (String)getCellValue(cell);
                System.out.println(value);
                if (value != null && !value.equals("")) {
                    list.add(value);
                }

            }
            if (list.size() > 0) {
                FilmArea filmArea = new FilmArea();
                filmArea.setFareaName(list.get(0));
                areaList.add(filmArea);
            }




        }
        filmAreaService.saveBatch(areaList);
        return R.ok().message("导入Excel成功");
    }



@GetMapping("exportToExcel")
public void exportToExcel(HttpServletResponse response) throws IOException {
    List<FilmArea> list = filmAreaService.list(null);
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet();
    String[] title = "编号，影片区域".split("，");
    Row row = sheet.createRow(0);
    Cell cell = null;
    for (int i = 0; i < title.length; i++) {
        cell = row.createCell(i);
        cell.setCellValue(title[i]);
    }
    for (int i = 0; i < list.size(); i++) {
        row = sheet.createRow(i + 1);
        cell = row.createCell(0);
        cell.setCellValue(list.get(i).getFareaId());
        cell = row.createCell(1);
        cell.setCellValue(list.get(i).getFareaName());

    }
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    workbook.write(os);
    new DownloadUtils().download(os,response,"影片区域.xlsx");
   /* String fileName = URLEncoder.encode("jjj.xlsx", "UTF-8");
    response.setContentType("application/octet-stream");
    response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO-8859-1")));
    response.setHeader("filename", fileName);
    workbook.write(response.getOutputStream());*/

}


    //根据单元格不同的数据类型获取数据
    public static Object getCellValue(Cell cell){
        CellType cellType = cell.getCellType();
        Object value = null;
        switch (cellType){
            case STRING:
                value = cell.getStringCellValue();
                break;
            case BOOLEAN:
                value = cell.getBooleanCellValue();
                break;
            case NUMERIC:
                if(DateUtil.isCellDateFormatted(cell)){
                    value = cell.getDateCellValue();
                }
                else {
                    value = cell.getNumericCellValue();//一定是double类型的
                }
            case FORMULA://公式
                value = cell.getCellFormula();
                break;
        }
        return value;
    }


}

